<template>
  <el-drawer class="container" size="800px" @close="closeForm('')" title="选择目标案件群" :visible.sync="visible" :wrapperClosable="false" :append-to-body="true">
    <div class="table" v-loading="loading">
      <div ref="queryform" class="queryform">
        <div class="clearfix">
          <div class="left item">
            <label class="label-size">分群名称：</label>
            <el-input
                size="small"
                style="width:240px !important;"
                placeholder="请输入"
                v-model="formField.name"
            ></el-input>
          </div>
          <div class="right item">
            <el-button @click="resetSearch" size="small" icon="el-icon-refresh" class="default-btn">重置</el-button>
            <el-button @click="searchList" size="small" icon="el-icon-search" class="plain-btn">查询</el-button>
          </div>
        </div>
      </div>
      <div class="table-body">
        <el-table :data="tableData" :border="false" style="width: 100%" >
          <el-table-column label="" width="55" align="center" fixed="left">
            <template slot-scope="scope">
              <el-radio @change="handleChange" v-model="tableradio" :label="scope.row"><i /></el-radio>
            </template>
          </el-table-column>
          <el-table-column prop="viewName" label="分群"></el-table-column>
          <el-table-column prop="peopleCoveredNum" label="覆盖人数"></el-table-column>
          <el-table-column prop="createUserName" label="创建人"></el-table-column>
          <el-table-column prop="createTime" label="创建时间" sortable></el-table-column>
        </el-table>
        <div class="clearfix" style="margin-top: 10px">
          <el-pagination
              style="text-align: center"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="pagination.pageIndex"
              :page-sizes="[10,20,30,40,50]"
              :page-size="pagination.pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="pagination.total"
          >
          </el-pagination>
        </div>
      </div>
    </div>
    <div class="drawer-footer">
      <el-button @click="handleOk" :loading="btnLoading" size="small" class="btn plain-btn">确定</el-button>
      <el-button @click="closeForm('')" size="small" class="btn default-btn">取消</el-button>
    </div>
  </el-drawer>
</template>

<script>
import {sandbox} from '@/libs/util'
import page from '@/mixins/pages.js'
import searchAndReset from '@/mixins/searchAndReset.js'
export default {
  name: "ChooseGroupDrawer",
  mixins: [page, searchAndReset],
  props:{
    visible: {
      type: Boolean,
      default: false,
    },
    mode: {
      type: String,
      default: '',
    },
    portraitType: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      loading:false,
      btnLoading:false,
      tableData: [],
      tableradio:'',
      selectRow:null,
      formField: sandbox({
        groupType: 'judicial_case',
        name: '',
        createUserName: '',
      }),
    }
  },
  watch:{
    visible(val){
      val && this.fetchData()
    },
  },
  methods: {
    LoadTableDataMethod() {
      return this.$api
          .api_urcap_group_queryPage({
            body: Object.assign(this.formField, this.pagination)
          })
          .then(res => {
            this.tableData = res.data.records
            this.pagination.currentPage = res.data.current
            this.pagination.pageSize = res.data.size
            this.pagination.total = res.data.total
          })
    },
    fetchData () {
      this.loading = true
      this.formField.pageIndex = this.pagination.pageIndex
      this.formField.pageSize = this.pagination.pageSize
      this.formField.groupType = this.portraitType

      return this.LoadTableDataMethod()
          .then(res => {
            if (!res) return
            this.tableData = res.Entity.Items || []
            this.pagination.pageIndex = res.Entity.pageIndex
            this.pagination.currentPage = res.Entity.pageIndex
            this.pagination.pageSize = res.Entity.pageSize
            this.pagination.total = res.Entity.TotalItemCount
          })
          .catch(() => {})
          .finally(() => {
            this.loading = false
          })
    },

    handleChange(row){
      this.selectRow = row
    },

    handleOk () {
      if(!this.selectRow){
        this.$message({
          type: 'warning',
          message: '请选择分群'
        })
        return
      }
      this.$emit('close',sandbox(this.selectRow))
    },

    closeForm(){
      this.$emit('close')
    },
  }
}
</script>

<style scoped>
.btn2{
  color: #2774F4;
  border-color: #2774F4
}
</style>